package m1;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws Exception {
        //建立连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140"); //www.wht6.cn
        //f.setPort(5672); //默认通信端口可以省略
        f.setUsername("admin");
        f.setPassword("admin");

        Channel c = f.newConnection().createChannel();

        //定义队列，要求服务器先把队列准备好
        c.queueDeclare("helloworld",false,false,false,null);

        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                byte[] a = message.getBody();
                String msg = new String(a);
                System.out.println("收到： "+msg);
            }
        };
        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {
            }
        };

        //从队列接收数据（消费数据）
        //c.basicConsume("helloworld",true,用来处理消息的回调对象,取消消息处理时的回调对象);
        c.basicConsume("helloworld",true,deliverCallback,cancelCallback);
    }
}
